﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLiNhaSachDTO;

namespace QuanLiNhaSachDAO
{
    public class TheLoaiDAO
    {
        public static DataTable selectAll()
        {
            string sql = "SELECT * FROM THELOAI";
            SqlDataAdapter da = new SqlDataAdapter(sql, sqlDB.MyConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static void insert(TheLoai m)
        {
            string sql = "INSERT INTO THELOAI(Ten) VALUES(@Ten)";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.Connection = sqlDB.MyConnection;
            cmd.Parameters.AddWithValue("@Ten", m.Ten);
            cmd.ExecuteNonQuery();
        }

        public static void update(TheLoai m)
        {
            string sql = "UPDATE THELOAI SET Ten = @Ten WHERE id = @id";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.Connection = sqlDB.MyConnection;
            cmd.Parameters.AddWithValue("@id", m.Id);
            cmd.Parameters.AddWithValue("@Ten", m.Ten);
            cmd.ExecuteNonQuery();
        }

        public static void delete(string id)
        {
            string sql = "DELETE FROM THELOAI WHERE id = @id";
            SqlCommand cmd = new SqlCommand(sql, sqlDB.MyConnection);
            cmd.Parameters.AddWithValue("@id", id);
            cmd.ExecuteNonQuery();
        }

        public static DataTable search(string id)
        {
            string sql = "SELECT * FROM THELOAI WHERE id ='" + id + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, sqlDB.MyConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable TimSachTuTheLoai(string id)
        {
            string sql = "SELECT * FROM SACH WHERE MaTheLoai = '" + id + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, sqlDB.MyConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;        
        }
    }
}
